﻿using System;
using System.Windows.Forms;
using ClickOnceMaster.Properties;
using System.IO;
using ClickOnceMaster.WizardConfiguration;

namespace ClickOnceMaster.Pages
{
    public partial class ApplicationPage : WizardPage
    {
        #region .ctors

        public ApplicationPage()
        {
            InitializeComponent();
            Text = Resources.ApplicationPage;
        }

        #endregion

        #region Instance methods

        #region WizardPage overrides

        /// <summary>
        /// Binds the page controls to the page data.
        /// </summary>
        protected override void BindPage()
        {
            base.BindPage();

            if (!string.IsNullOrEmpty(WizardSettings.Current.Manifest.KeyPath) &&
                File.Exists(WizardSettings.Current.Manifest.KeyPath))
            {
                tbSignPath.Text = WizardSettings.Current.Manifest.KeyPath;
            }
            else
            {
                tbSignPath.Text = WizardSettings.Current.Manifest.KeyPath = string.Empty;
            }

            if (!string.IsNullOrEmpty(tbDestinationFolder.Text = WizardSettings.Current.Manifest.DestinationFilesPath) &&
                Directory.Exists(tbDestinationFolder.Text = WizardSettings.Current.Manifest.DestinationFilesPath))
            {
                tbDestinationFolder.Text = WizardSettings.Current.Manifest.DestinationFilesPath;
            }
            else
            {
                tbDestinationFolder.Text = WizardSettings.Current.Manifest.DestinationFilesPath = string.Empty;
            }

            tbApplicationName.Text = WizardSettings.Current.Manifest.ApplicationName;
            tbApplicationPublisher.Text = WizardSettings.Current.Manifest.ApplicationPublisher;

            tbMajor.Value = WizardSettings.Current.Manifest.PrimaryNumber;
            tbMinor.Value = WizardSettings.Current.Manifest.AdditionalNumber;
            tbBuild.Value = WizardSettings.Current.Manifest.ConstructionNumber;
            tbRevision.Value = WizardSettings.Current.Manifest.ReleaseNumber;
        }

        /// <summary>
        /// Returns a boolean value which indicates whether the controls within the page filled up correctly by a user.
        /// </summary>
        /// <returns>If the page controls' values are valid returns <c>true</c>; otherwise - <c>false</c>.</returns>
        public override bool ValidatePage()
        {
            bool result = true;
            Validator.Clear();

            if (string.IsNullOrEmpty(WizardSettings.Current.Manifest.KeyPath) ||
                !File.Exists(WizardSettings.Current.Manifest.KeyPath))
            {
                result = false;
                Validator.SetError(tbSignPath, Resources.Validation_KeyNotFound);
            }

            if (string.IsNullOrEmpty(WizardSettings.Current.Manifest.DestinationFilesPath) ||
                !Directory.Exists(WizardSettings.Current.Manifest.DestinationFilesPath))
            {
                result = false;
                Validator.SetError(tbSignPath, Resources.Validation_DirectoryNotFound);
            }

            if (string.IsNullOrEmpty(WizardSettings.Current.Manifest.ApplicationName))
            {
                result = false;
                Validator.SetError(tbApplicationName, Resources.Validation_AppName);
            }

            if (string.IsNullOrEmpty(WizardSettings.Current.Manifest.ApplicationPublisher))
            {
                result = false;
                Validator.SetError(tbApplicationPublisher, Resources.Validation_AppPublisher);
            }

            return base.ValidatePage() && result;
        }

        #endregion

        #region Event handlers

        private void bsLoadFile_Click(object sender, EventArgs e)
        {
            // Prompt for cert file
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = Resources.KeyFilesFilter;

            // Restore last location if available
            if (!string.IsNullOrEmpty(WizardSettings.Current.Manifest.KeyPath) &&
                Directory.Exists(Path.GetDirectoryName(WizardSettings.Current.Manifest.KeyPath)))
            {
                ofd.InitialDirectory = Path.GetDirectoryName(WizardSettings.Current.Manifest.KeyPath);
            }

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbSignPath.Text = ofd.FileName;
                // Save selected location and path for next time
                WizardSettings.Current.Manifest.KeyPath = ofd.FileName;
            }
        }

        private void btChangeDestinationFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = WizardSettings.Current.Manifest.DestinationFilesPath;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                WizardSettings.Current.Manifest.DestinationFilesPath = tbDestinationFolder.Text = dialog.SelectedPath;
            }
        }

        private void tbMajor_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown control = sender as NumericUpDown;
            if (control != null)
            {
                WizardSettings.Current.Manifest.PrimaryNumber = Convert.ToInt32(control.Value);
            }
        }

        private void tbMinor_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown control = sender as NumericUpDown;
            if (control != null)
            {
                WizardSettings.Current.Manifest.AdditionalNumber = Convert.ToInt32(control.Value);
            }
        }

        private void tbBuild_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown control = sender as NumericUpDown;
            if (control != null)
            {
                WizardSettings.Current.Manifest.ConstructionNumber = Convert.ToInt32(control.Value);
            }
        }

        private void tbRevision_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown control = sender as NumericUpDown;
            if (control != null)
            {
                WizardSettings.Current.Manifest.ReleaseNumber = Convert.ToInt32(control.Value);
            }
        }

        private void tbApplicationName_TextChanged(object sender, EventArgs e)
        {
            TextBox control = sender as TextBox;
            if (control != null)
            {
                WizardSettings.Current.Manifest.ApplicationName = control.Text;
            }
        }

        private void tbApplicationPublisher_TextChanged(object sender, EventArgs e)
        {
            TextBox control = sender as TextBox;
            if (control != null)
            {
                WizardSettings.Current.Manifest.ApplicationPublisher = control.Text;
            }
        }

        #endregion

		

        #endregion
    }
}